package com.wing.purchase.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.enums.PurEnum;
import com.wing.web.db.config.util.DynamicDataSource;
import com.wing.purchase.model.response.PurchaseDeliveryRecordVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.purchase.model.entity.PurchaseDeliveryRecord;
import com.wing.purchase.model.request.PurchaseDeliveryRecordForm;
import com.wing.purchase.service.PurchaseDeliveryRecordService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 供应商发货记录管理
 *
 * @author xiayang
 */
@Validated
@Api(value = "供应商发货记录管理", tags = "#/purchase/purchaseDeliveryRecord")
@RestController("purchaseDeliveryRecordController")
@RequestMapping("purchase/admin/purchaseDeliveryRecord")
@RequiredArgsConstructor
public class PurchaseDeliveryRecordController {

    private final PurchaseDeliveryRecordService purchaseDeliveryRecordService;

    @ApiOperation(value = "查询所有供应商发货记录", notes = "搜索")
    @GetMapping("/list")
    public PageResult<PurchaseDeliveryRecordVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(name = "corporateName", defaultValue = "", required = false) String corporateName,
            @RequestParam(name = "businessContact", defaultValue = "", required = false) String businessContact,
            @RequestParam(name = "phone", defaultValue = "", required = false) String phone,
            @RequestParam(name = "id", defaultValue = "", required = false) String id,
            @RequestParam(defaultValue = "desc") String order, Integer filterWarehousingComplete,Integer supplierId) {
        Page<PurchaseDeliveryRecordVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        params.put("sort", sort);
        params.put("order", order);
        params.put("corporateName", corporateName);
        params.put("businessContact", businessContact);
        params.put("phone", phone);
        params.put("filterWarehousingComplete", filterWarehousingComplete);
        params.put("supplierId", supplierId);

        if(StringUtils.isNotBlank(id)){
            params.put("id", id);
        }

        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);
        Page<PurchaseDeliveryRecordVO> list = purchaseDeliveryRecordService.pageList4app(pg, params);
        list.getRecords().forEach(info -> {
            info.setStatusName(PurEnum.sendStatus.getNameByCode(info.getStatus()));
        });
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加供应商发货记录", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@Validated(PurchaseDeliveryRecordForm.Add.class) @RequestBody PurchaseDeliveryRecordForm purchaseDeliveryRecordForm) {
        if("SUPPLIER_USER".equals(JwtUtils.getRoleCode())){
            purchaseDeliveryRecordForm.setEnterType("2");
        }else{
            purchaseDeliveryRecordForm.setEnterType("1");
        }

        //校验 waybillNo
        if(com.alibaba.nacos.common.utils.StringUtils.isEmpty(purchaseDeliveryRecordForm.getWaybillNo())){
            return JsonResult.fail("运单编号为空");
        }

        if(com.alibaba.nacos.common.utils.StringUtils.isEmpty(purchaseDeliveryRecordForm.getLogisticsCompany())){
            return JsonResult.fail("物流公司为空");
        }


        if (purchaseDeliveryRecordService.add(purchaseDeliveryRecordForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改供应商发货记录", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@RequestBody PurchaseDeliveryRecordForm purchaseDeliveryRecordForm) {
        PurchaseDeliveryRecord purchaseDeliveryRecord = purchaseDeliveryRecordService.getById(purchaseDeliveryRecordForm.getId());
        if (purchaseDeliveryRecord == null) {
            return JsonResult.fail("修改失败！");
        }
        if (purchaseDeliveryRecordService.update(purchaseDeliveryRecordForm) > 0) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "删除供应商发货记录", notes = "删除")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long purchaseDeliveryRecordId) {
        if (purchaseDeliveryRecordService.removeById(purchaseDeliveryRecordId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "供应商发货记录详情", notes = "详情")
    @GetMapping("/get")
    public JsonResult get(@RequestParam("id") Long id) {
        return JsonResult.success(purchaseDeliveryRecordService.get4appById(id));
    }

    @ApiOperation(value = "查询供应商运单编号关联信息", notes = "运单列表")
    @GetMapping("/getWaybillInfoBy")
    public JsonResult getWaybillInfoBy(@RequestParam("supplierId") String supplierId) {
        return JsonResult.success(purchaseDeliveryRecordService.getWaybillInfoBy(supplierId));
    }

    @ApiOperation(value = "供应商发货记录详情2.0", notes = "详情")
    @GetMapping("/detail/{id}")
    public JsonResult detail(@PathVariable("id") Long id) {
        PurchaseDeliveryRecordVO purchaseDeliveryRecordVO = purchaseDeliveryRecordService.detail(id);
        return JsonResult.success(purchaseDeliveryRecordVO);
    }

}
